<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/2/28
 * Time: 16:31
 */

namespace App\Http\Logics\Admin;


use App\Train;
use App\Http\Logics\BaseLogic;
class TrainLogic extends BaseLogic
{
    public static function createTrain($data)
    {
        $train = new Train();
        $train->cafe_id = $data['cafe_id'];
        $train->name = $data['name'];
        $train->num = $data['num'];
        $train->cost = $data['cost'];
        $train->link_man = $data['link_man'];
        $train->mobile = $data['mobile'];
        $train->mark = $data['mark'];
        if (!$train->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS, $train->id);
    }

    public static function updateTrain($data)
    {
        $train = Train::find($data['train_id']);
        if (empty($train)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $train->name = $data['name'];
        $train->num = $data['num'];
        $train->cost = $data['cost'];
        $train->link_man = $data['link_man'];
        $train->mobile = $data['mobile'];
        $train->mark = $data['mark'];
        if (!$train->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function deleteTrain($data)
    {
        $train = Train::find($data['train_id']);
        if (empty($train)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        if (!$train->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }

    public static function getTrainList($data)
    {
        $query = Train::with(['trainContent'])->where(function ($query) use ($data){
            if (isset($data['status']) && $data['status'] != ''){
                $query->where('status', $data['status']);
            }
        })->where('cafe_id', $data['cafe_id']);
        $train = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        if (empty($train)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $train);
    }

    public static function getTrainDetail($data)
    {
        $train = Train::with(['trainContent'])->find($data['train_id']);
        if (empty($train)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $train);
    }

    /**
     * 改变培训信息的状态
     * @param $data
     * @return mixed
     */
    public static function changeStatus($data)
    {
        $train = Train::find($data['train_id']);
        if (empty($train)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $train->status = !$train->status;
        if (!$train->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }
}